#!/bin/bash

# Get the list of current connections
connections=$(nmcli dev | grep 'wifi' | awk '{print $4}')

flag=false
for con in $connections; do
    # Check if there is a Wi-Fi connection
    if [[ -z "$con" || "$con" == "--" ]]; then
        echo "No Wi-Fi."
        # systemctl start KlipperScreen
    else
        echo "Found the following Wi-Fi connections: $con"
        flag=true
        sudo sleep 2
        sudo systemctl stop KlipperScreen.service
        export XDG_RUNTIME_DIR=/root
        export XDG_CONFIG_HOME=/root/.config
        weston-launch < /dev/tty1 > /dev/tty1 2>&1 &
        sleep 2
        /home/umeko/.KlipperScreen-env/bin/python /home/umeko/KlipperScreen/screen.py
    fi
done
if [[ "$flag" == "false" ]]; then
    echo "No Wi-Fi connection found."
    export Environment="KS_XCLIENT=/home/umeko/.KlipperScreen-env/bin/python /home/umeko/KlipperScreen/screen.py"
    cd /home/umeko/KlipperScreen
    # /home/umeko/KlipperScreen/scripts/KlipperScreen-start.sh
    sudo systemctl start KlipperScreen.service
fi


